#include <stdio.h>
#include <string.h>
#include "spf/spf.h"



int main()
{

	// spf encode sample
	spf_t g_spf;
	int status = spf_encode_init(&g_spf, 256);
	if (status != 0) {
		printf("spf_encode_init error\n");
		exit();
	}

	status = spf_encode_add_string(&g_spf, "name", "ZhangSan");
	if (status != 0) {
		printf("spf_encode_add_string error\n");
		exit();
	}

	status = spf_encode_add_int(&g_spf, "age", 21);
	if (status != 0) {
		printf("spf_encode_add_int error\n");
		exit();
	}

	status = spf_encode_add_float(&g_spf, "money", 12.369);
	if (status != 0) {
		printf("spf_encode_add_float error\n");
		exit();
	}
	printf("%s\n", g_spf.payload);
	spf_encode_deinit(&g_spf);

	char* payload = "name:ZhangSan;age:21;money:12.369000;";

	// spf decode sample
	spf_t d_spf;
	status = spf_decode_init(&d_spf, payload, strlen(payload));
	if (status != 0) {
		printf("spf_decode_init error\n");
		return;
	}

	int age = 0;
	status = spf_decode_get_int(&d_spf, "age", &age);
	if (status != 0) {
		printf("spf_decode_get_int error\n");
		return;
	}
	printf("%s:%d\n", "age", age);


	float money = 0;
	status = spf_decode_get_float(&d_spf, "money", &money);
	if (status != 0) {
		printf("spf_decode_get_float error\n");
		return;
	}
	printf("%s:%f\n", "money", money );


	char name[16] = { 0 };
	status = spf_decode_get_string(&d_spf, "name", name, 16);
	if (status != 0) {
		printf("spf_decode_get_string error\n");
		return;
	}
	printf("%s:%s\n", "name", name);

	spf_decode_deinit(&d_spf);

	return 0;
}